import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common';
import { Request } from 'express';
import business from './common/business';
import { UserToken } from './entities/user.token';

@Injectable()
export class TokenGuard implements CanActivate {
    canActivate(context: ExecutionContext) {
        const request = context.switchToHttp().getRequest<Request>();
        let authorization = request.header(`tokenauthorization`);
        if (authorization == null || authorization === '') {
            return false;
        }
        let user: UserToken = business.jwtDecrypt(authorization.toString());
        if (user == null || user.expireTime < business.getNow()) {
            return false;
        }
        return true;
    }
}
